import 'package:flutter/material.dart';
import 'package:chathouse/public/Config.dart';
import 'MinLoadingView.dart';

// 视图状态
enum LoadState { success, loading, error, empty, minLoading }

class LoadLayoutPage extends StatefulWidget {
  LoadState? state; //页面状态
  VoidCallback? errorRetry; //错误事件处理
  VoidCallback? emptyRetry; //空数据事件处理
  LoadLayoutPageState? page;

  String? imgPath;
  String? tipTitle;

  bool? isShowBackBtn;

  LoadLayoutPage(
      {Key? key,
      this.state = LoadState.loading,
      this.errorRetry,
      this.emptyRetry,
      this.imgPath,
      this.isShowBackBtn = false,
      this.tipTitle})
      : super(key: key);

  @override
  LoadLayoutPageState createState() {
    page = LoadLayoutPageState();
    return page!;
  }
}

class LoadLayoutPageState extends State<LoadLayoutPage> {
  updateLoadLayout() {
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      constraints: BoxConstraints.expand(),
      child: _buildWidget,
    );
  }

  ///根据不同状态来显示不同的视图
  Widget get _buildWidget {
    switch (widget.state) {
      case LoadState.error:
        return ErrorPage(
          // isShowBackBtn: widget.isShowBackBtn ?? true,
          isShowBackBtn:widget.isShowBackBtn!,
          errorRetry: widget.errorRetry,
        );
      case LoadState.loading:
        return LoadingView();
      case LoadState.minLoading:
        return MinLoadingView();
      case LoadState.empty:
        return EmptyPage(
          emptyRetry: widget.emptyRetry,
          imgName: widget.imgPath,
          tipTitle: widget.tipTitle,
        );
      default:
        return Container();
    }
  }
}
